package com.geek.water.controller;

import com.geek.water.constant.MessageConstant;
import com.geek.water.constant.CodeConstant;
import com.geek.water.constant.RedisImgConstant;
import com.geek.water.result.Result;
import com.geek.water.service.impl.FileUploadContext;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Set;

/**
 * @author chenmin
 * @desc TODO
 * @date 2025/7/28
 */
@RestController
@RequestMapping("/admin/file")
@RequiredArgsConstructor
@Slf4j
public class FileController {

    private final FileUploadContext fileUploadContext;
    private final RedisTemplate<String, Object> redisTemplate;

    @PostMapping("upload")
    public Result upload(@RequestParam("provider") String provider, @RequestParam("file") MultipartFile file) {
        try {
            System.out.println("接收到的provider: " + provider);
            System.out.println("接收到的文件: " + (file != null ? file.getOriginalFilename() : "null"));

            if (file == null || file.isEmpty()) {
                return new Result(CodeConstant.FAIL, "文件不能为空");
            }

            String url = fileUploadContext.upload(file, provider);
            System.out.println("上传结果URL: " + url);

            if (StringUtils.hasLength(url)) {
                // 记录上传的图片到Redis，用于后续清理垃圾图片
                // 将图片URL添加到所有上传图片的集合中
                redisTemplate.opsForSet().add(RedisImgConstant.UPLOAD_PIC, url);
                // 记录图片对应的云服务商信息，用于删除时指定正确的删除策略
                redisTemplate.opsForValue().set(url + "_provider", provider);

                return new Result(CodeConstant.SUCCESS, MessageConstant.PIC_UPLOAD_SUCCESS, url);
            }
            return new Result(CodeConstant.FAIL, MessageConstant.PIC_UPLOAD_FAIL);
        } catch (Exception e) {
            System.err.println("文件上传异常: " + e.getMessage());
            e.printStackTrace();
            return new Result(CodeConstant.FAIL, "文件上传失败: " + e.getMessage());
        }
    }

    @PostMapping("delete")
    public Result delete(@RequestParam("provider") String provider, @RequestParam("url") String url) {
        try {
            boolean flag = fileUploadContext.delete(url, provider);
            if (flag) {
                // 删除成功后，从Redis中清理相关记录
                redisTemplate.opsForSet().remove(RedisImgConstant.UPLOAD_PIC, url);
                redisTemplate.opsForSet().remove(RedisImgConstant.UPLOAD_DB_PIC, url);
                redisTemplate.delete(url + "_provider");

                return new Result(CodeConstant.SUCCESS, MessageConstant.PIC_DELETE_SUCCESS);
            }
            return new Result(CodeConstant.FAIL, MessageConstant.PIC_DELETE_FAIL);
        } catch (Exception e) {
            System.err.println("文件删除异常: " + e.getMessage());
            e.printStackTrace();
            return new Result(CodeConstant.FAIL, "文件删除失败: " + e.getMessage());
        }
    }


}